# -*- coding: utf-8 -*-
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db

from emof import core as EMOF
from emof import dumper
from emof import loader

from model import Model

class LoadAllElements(webapp.RequestHandler):
    def get(self):
        m_id = self.request.get("m_id")
        
        model = Model.get(m_id)

        elements = list()
        
        rep = loader.Loader().load(model.model_XMI)        
        
        for pt in rep.PrimitiveType:
            if not (isinstance(pt, EMOF.Boolean) or isinstance(pt, EMOF.Integer) or isinstance(pt, EMOF.String) or isinstance(pt, EMOF.UnlimitedNatural)):
                elements.append(pt)
            
        for enum in rep.Enumeration:
            elements.append(enum)
                            
        for clss in rep.Class:
            elements.append(clss)
        
        for pkg in rep.Package:
            elements.append(pkg)
        
        for op in rep.Operation:
            elements.append(op)
        
        for pa in rep.Parameter:
            elements.append(pa)
        
        for pr in rep.Property:
            elements.append(pr)
        
        for el in rep.EnumerationLiteral:
            elements.append(el)
        
        for tg in rep.Tag:
            elements.append(tg)
        
        for cm in rep.Comment:
            elements.append(cm)
        
        template_values = {'elements': elements,
                           'id': id}
        
        path = os.path.join(os.getcwd(), 'xml/getElements.xml')
        self.response.headers['Content-Type'] = 'application/xml'
        self.response.out.write(template.render(path, template_values))
        